//
//  UIView+GXAddition.m
//  

#import "UIView+GXAddition.h"

@implementation UIView (GXAddition)

#pragma mark - setter & getter

// x
- (CGFloat)gx_X {
    return self.frame.origin.x;
}

- (void)setGx_X:(CGFloat)gx_X {
    CGRect gxFrame = self.frame;
    gxFrame.origin.x = gx_X;
    self.frame = gxFrame;
}

// y
- (CGFloat)gx_Y {
    return self.frame.origin.x;
}

- (void)setGx_Y:(CGFloat)gx_Y {
    CGRect gxFrame = self.frame;
    gxFrame.origin.y = gx_Y;
    self.frame = gxFrame;
}

// width
- (CGFloat)gx_Width {
    return self.frame.size.width;
}

- (void)setGx_Width:(CGFloat)gx_Width {
    CGRect gxFrame = self.frame;
    gxFrame.size.width = gx_Width;
    self.frame = gxFrame;
}

// height
- (CGFloat)gx_Height {
    return self.bounds.size.height;
}

- (void)setGx_Height:(CGFloat)gx_Height {
    CGRect gxFrame = self.frame;
    gxFrame.size.height = gx_Height;
    self.frame = gxFrame;
}

// frame.origin
- (CGPoint)gx_Origin {
    return self.frame.origin;
}

- (void)setGx_Origin:(CGPoint)gx_Origin {
    self.gx_X = gx_Origin.x;
    self.gx_Y = gx_Origin.y;
}

// frame.size
- (CGSize)gx_Size {
    return self.frame.size;
}

-(void)setGx_Size:(CGSize)gx_Size {
//    CGRect gxFrame = self.frame;
//    gxFrame.size = gx_Size;
//    self.frame = gxFrame;

    self.gx_Width = gx_Size.width;
    self.gx_Height = gx_Size.height;
}

// center.x
- (CGFloat)gx_CenterX {
    return self.center.x;
}

- (void)setGx_CenterX:(CGFloat)gx_CenterX {
    self.center = CGPointMake(gx_CenterX, self.center.y);
}

// center.y
- (CGFloat)gx_CenterY {
    return self.center.y;
}

- (void)setGx_CenterY:(CGFloat)gx_CenterY {
    self.center = CGPointMake(self.center.x, gx_CenterY);
}


#pragma mark - Methods

- (UIImage *)gx_snapshot:(BOOL)opaque {
    //
    UIGraphicsBeginImageContextWithOptions(self.gx_Size, opaque, 0.0);
    [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
    
//    CGContextRef ctx = UIGraphicsGetCurrentContext();
//    [self.layer renderInContext:ctx];
    
    UIImage * resultImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return resultImg;
}

- (void)gx_nineBoxAlgorithm:(NSArray<UIView *> *)itemArr marginTop:(CGFloat)marginTop itemMargin:(CGFloat)itemMargin itemWidth:(CGFloat)width itemHeight:(CGFloat)height columCount:(NSInteger)columCount {
    //
    CGFloat x, y;
    CGRect itemFrame = CGRectZero;
    
    // itemMargin 和 itemWidth 不能同时为0 。
    // itemMargin 和 itemHeight 不能同时为0 。
    
    if (itemMargin == 0) {
        itemMargin = (self.gx_Width - columCount * width) / (columCount + 1);
    }
    
    if (width == 0) {
        width = (self.gx_Width - (columCount + 1) * itemMargin) / columCount;
    }
    
    int rowIndex, colIndex;
    
    for (int i = 0; i < itemArr.count; i++) {
        //
        rowIndex = i / columCount;
        colIndex = i % columCount;
        
        x = itemMargin + colIndex * (width + itemMargin);
        y = marginTop + itemMargin + rowIndex * (height + itemMargin);
        
        itemFrame = CGRectMake(x, y, width, height);
        
        itemArr[i].frame = itemFrame;
    } // for end 
}

- (void)gx_shadowWithColor:(UIColor *)color shadowRadius:(CGFloat)shadowRadius shadowOpacity:(CGFloat)shadowOpacity {
    //
//    UIBezierPath * shadowPath = [UIBezierPath bezierPath];
//    [shadowPath moveToPoint:CGPointMake(self.gx_X - 2, self.gx_Y - 2)];
//    [shadowPath addLineToPoint:CGPointMake(self.gx_X - 2, self.gx_Height + 2)];
//    [shadowPath addLineToPoint:CGPointMake(self.gx_Width + 2, self.gx_Height + 2)];
//    [shadowPath addLineToPoint:CGPointMake(self.gx_Width + 2, self.gx_Y - 2)];
//    [shadowPath closePath];
    
    self.layer.shadowColor = color.CGColor;
    self.layer.shadowOpacity = 0.6f;
    self.layer.shadowRadius = 10;
    self.layer.shadowOffset = CGSizeMake(2, 2);
//    self.layer.shadowPath = shadowPath.CGPath;
}


@end
